package com.sgb.packers.view.adapter;

import android.util.TypedValue;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.sgb.link.router.PathTeam;
import com.sgb.packers.R;
import com.sgb.packers.databinding.PackersItemWorkAccountBinding;
import com.sgb.packers.entity.WorkTakeEntity;
import com.swgk.core.base.model.entity.RouterKey;

import java.util.List;
import java.util.Locale;

/**
 * @author 王彬臣
 * @date 2021/11/24
 * @Description 记工流水列表
 */
public class WorkAccountAdapter extends BaseQuickAdapter<WorkTakeEntity, BaseViewHolder> {


    public WorkAccountAdapter(@Nullable List<WorkTakeEntity> data) {
        super(R.layout.packers_item_work_account, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, WorkTakeEntity item) {
        PackersItemWorkAccountBinding mBinding = DataBindingUtil.bind(helper.itemView);
        TextView tv = helper.itemView.findViewById(R.id.tv_num);
        tv.setText(String.format(Locale.CHINA, "%s (%d笔)", item.getRecordDate(), item.getList().size()));
        assert mBinding != null;
        mBinding.rvList.setLayoutManager(new LinearLayoutManager(mContext));
        WorkAccountChildAdapter mAdapter = new WorkAccountChildAdapter(item.getList());
        if (mBinding.rvList.getItemDecorationCount() == 0) {
            DividerDecoration.builder(mContext)
                    .color(mContext.getResources().getColor(R.color.color_f5f5f5))
                    .insets(15, 15)
                    .size(1, TypedValue.COMPLEX_UNIT_DIP)
                    .build()
                    .addTo(mBinding.rvList);
        }
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            WorkTakeEntity.ListDTO data = item.getList().get(position);
            if ("1".equals(data.getrType())) {
                ARouter.getInstance().build(PathTeam.WorkOrContractorDetailActivity).withString(RouterKey.NO, data.getWorkRecordNo()).withBoolean(RouterKey.BOOLEAN, true).navigation();
            } else {
                ARouter.getInstance().build(PathTeam.WorkOrContractorDetailActivity).withString(RouterKey.NO, data.getWorkRecordNo()).withBoolean(RouterKey.BOOLEAN, false).navigation();
            }
        });
        mAdapter.bindToRecyclerView(mBinding.rvList);
    }
}
